package com.futureCloudMOM.controller.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.futureCloudMOM.common.annotation.Log;
import com.futureCloudMOM.common.core.controller.BaseController;
import com.futureCloudMOM.common.core.domain.AjaxResult;
import com.futureCloudMOM.common.core.page.TableDataInfo;
import com.futureCloudMOM.common.utils.core.QueryWapperDTO;
import com.futureCloudMOM.common.utils.core.QueryWapperUtils;
import com.futureCloudMOM.core.base.service.BaseApiExt;
import com.futureCloudMOM.core.base.service.BaseService;
import com.futureCloudMOM.core.entity.FBaseEntity;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

;

/**
 * 文件名 BaseApiController
 *
 * @since 2023/5/13 13:57
 */
@Slf4j
@AllArgsConstructor
public abstract class BaseApiController<Entity extends FBaseEntity, Dto extends Entity, Service extends BaseService<Entity>> extends BaseController implements BaseApiExt<Entity, Dto> {

    protected Service service;

    @ApiOperation("通用API - 数据分页")
    @Log(title = "列表页数据查询")
    @PostMapping("/getList")
//    @Cacheable(key = "#entity + (#httpRequest.getParameter('pageSize') + (#httpRequest.getParameter('pageNum')))",cacheNames = "test")
    public TableDataInfo getList(Entity entity, @RequestBody(required = false) List<QueryWapperDTO> list) {
        startPage();
        QueryWrapper<Entity> queryWrapper = QueryWapperUtils.wapperJojnt(entity, list);
        queryWrapper.eq("deleted", 0);
        queryWrapper.orderByDesc("sysCreateTime");
        return getDataTable(service.list(queryWrapper));
    }

    @Log(title = "通用API - 根据ID查询单条数据")
    @ApiOperation("通用API - 根据ID查询单条数据")
    @GetMapping("/getEntityById")
    public AjaxResult getEntityById(Long id) {
        return success(service.getById(id));
    }

    @Log(title = "通用API - 添加数据")
    @ApiOperation("通用API - 添加数据")
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody Dto dto) {
        service.save(dto);
        addExt(dto);
        return success();
    }

    @Log(title = "通用API - 修改数据")
    @ApiOperation("通用API - 修改数据")
    @Transactional
    @PutMapping
    public AjaxResult edit(@RequestBody Dto dto) {
        service.updateById(dto);
        updateExt(dto);
        return success(dto);
    }

    @Log(title = "通用API - 根据ID删除单条数据")
    @ApiOperation("通用API - 根据ID删除单条数据")
    @Transactional
    @DeleteMapping
    public AjaxResult delete(@RequestParam("id") Long id) {
        service.removeById(id);
        deleteExt(new Long[]{id});
        return success();
    }

    @Log(title = "通用API - 批量删除数据")
    @ApiOperation("通用API - 批量删除数据")
    @Transactional
    @DeleteMapping("batch")
    public AjaxResult batch(@RequestParam("ids") Long[] ids) {
        if (ArrayUtils.isEmpty(ids)) {
            return error("未携带删除参数");
        }
        if (ids.length == 1) {
            return delete(ids[0]);
        }
        service.removeByIds(Collections.singleton(ids));
        deleteExt(ids);
        return success();
    }

    @Log(title = "通用API - excel导出")
    @ApiOperation("通用API - excel导出（暂未实现）")
    @GetMapping("export")
    public AjaxResult export(Entity entity, HttpServletRequest request) {
        return success();
    }

    @Log(title = "通用API - 导入 excel")
    @ApiOperation("通用API - 导入 excel（暂未实现）")
    @PostMapping("import")
    public AjaxResult importExcel(HttpServletRequest request, HttpServletResponse response) {
        return success();
    }

}
